<?php
/**
 * 客户
 * @author Administrator
 *
 */
class Application_Model_DbTable_Customer extends Application_Model_DbTable_Base
{
    protected $_name = 'customer';

	/**
	 * 取得客户列表
	 */
	public function getUserListForFlex($pageM = NULL){
		$rows = $this->jqfetchAll($pageM);
		return $rows;
	}


	/**
	 * 客户id-名
	 * @return array
	 */
	public function getKvIdName(){
		$rows = $this->fetchAll();
		$data = array();
		foreach ($rows as $r){
			$data[$r->id] = $r->name;
		}
		return $data;
	}
	
	/**
	 * 根据email获取customer
	 * @param string $mail
	 * @return Zend_Db_Table_Row_Abstract|null  
	 */
	public function getItemByMail($mail){
		$select = $this->select();
		$select->where(" `email`=?",$mail);
		$select->orWhere(" `email2`=? ",$mail);
		return $this->fetchRow($select);
	}
	
	/**
	 * 统一的添加下拉列表项目
	 * @param Application_Form_Base $formM
	 */
	public function setMultiOptions(Application_Form_Base $formM){
		//添加客户
		$customerE = $formM->getElement("customer");
		if($customerE){
			//$customerM = new Application_Model_DbTable_Customer();
			$customerL = $this->fetchAll();
			$customerD = array();
			foreach ($customerL as $customer){
				$customerD[$customer["id"]] = $customer["name"] ;
			}
			$customerE->setMultiOptions($customerD);
		}
		return $formM;
	}
	
	
	/**
	 * 有过跟进记录的客户，已开发客户
	 * @return array
	 */
    public function getItemsHasMsg(){
    	$msgM = new Application_Model_DbTable_Msg();
    	$select = $msgM->select();
    	$select->from($msgM->getTableName(),"foreign_value AS customer");
    	$select->distinct();
    	$select->where(" `foreign_key` = 'customer' ");
    	$hasML = $msgM->fetchAll($select);
    	$hasML = $hasML->toArray();
    	$hl2 = array();
    	foreach ($hasML as $h){
    		$hl2[] = $h["customer"];
    	}
    	$hl2[] = 0;
    	return $hl2;
    }
    
	/**
	 * 正在跟进的客户，
	 * @return array
	 */
    public function getItemsDoingMsg(){
    	$msgM = new Application_Model_DbTable_Msg();
    	$select = $msgM->select();
    	$select->from($msgM->getTableName(),"foreign_value AS customer");
    	$select->distinct();
    	$select->where(" `foreign_key` = 'customer' ");
    	$select->where(" `state` <> 1 ");
    	$hasML = $msgM->fetchAll($select);
    	$hasML = $hasML->toArray();
    	$hl2 = array();
    	foreach ($hasML as $h){
    		$hl2[] = $h["customer"];
    	}
    	$hl2[] = 0;
    	return $hl2;
    }
    

	/**
	 * 有报价单的客户，
	 * @return array
	 */
    public function getItemsHasQuote(){
    	$quoteM = new Application_Model_DbTable_Quote();
    	$select = $quoteM->select();
    	$select->from($quoteM->getTableName(),"customer AS customer");
    	$select->distinct();
    	$select->where(" `customer` IS NOT NULL ");
    	$select->where(" `state` <> 1 ");
    	$hasML = $quoteM->fetchAll($select);
    	$hasML = $hasML->toArray();
    	$hl2 = array();
    	foreach ($hasML as $h){
    		$hl2[] = $h["customer"];
    	}
    	$hl2[] = 0;
    	return $hl2;
    }
}

